package module.building.view.buildingConfirmPanel
{
	import com.darwin.darwinUI.containers.Container;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.controls.LabelButton;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.core.UISprite;
	
	import flash.events.MouseEvent;
	
	import module.building.event.BuildEvent;
	import module.main.view.ResourceConst;

	public class BuildingConfirmPanel extends UIComponent
	{
		private var buttonContainer:UISprite;
		private var label:Label;
		private var _type:int;
		
		/**
		 * 建筑提示框 
		 * @param type 0是“是否建造”；1是“是否撤消”；2是“是否拆除”
		 */
		public function BuildingConfirmPanel(type:int)
		{
			super();
			_type = type;
			init();
			initData();
		}
		
		private function init():void
		{
			this.backgroundSkin = ResourceConst.BUILD_IMAGE_FRAME_BG;
			label = new Label();
			
			var confirmButton:LabelButton = new LabelButton(CONFIRM,null,ResourceConst.BUILD_IMAGE_TAB_BG);
			confirmButton.backgroundAuto = true;
			var cancelButton:LabelButton = new LabelButton(CANCEL,null,ResourceConst.BUILD_IMAGE_TAB_BG);
			cancelButton.backgroundAuto = true;
			buttonContainer = new UISprite();
			buttonContainer.addChild(confirmButton);
			buttonContainer.addChild(cancelButton);
			
			addChild(label);
			addChild(buttonContainer);
		}
		
		private function initData():void
		{
			background.width = 300;
			background.height = 200;
			this.width = background.width;
			this.height = background.height;
			
			label.text = TIP_ARRAY[_type];
			label.x = (width - label.textWidth)/2;
			label.y = (height - label.textHeight)/2 - 30;
			
			for(var i:int=0;i<buttonContainer.numChildren;i++)
			{
				var btn:LabelButton = buttonContainer.getChildAt(i) as LabelButton
				btn.x = i*110;
				btn.width = btn.background.width;
				btn.height = btn.background.height;
				btn.textX = (btn.width-btn.textWidth)/2;
				btn.textY = 5;
				btn.addEventListener(MouseEvent.CLICK, buttonClickHandler);
			}
			
			buttonContainer.x = (width - buttonContainer.width)/2;
			buttonContainer.y = (height - buttonContainer.height) - 20;
		}
		
		private function buttonClickHandler(e:MouseEvent):void
		{
			var obj:Object = BUTTON_ARRAY[_type];
			var evtString:String;
			if(e.currentTarget.text == CONFIRM)
			{
				evtString = obj["CONFIRM"];
			}else if(e.currentTarget.text == CANCEL)
			{
				evtString = obj["CANCEL"];
			}
			var event:BuildEvent = new BuildEvent(evtString);
			if(event){dispatchEvent(event);}
		}
		
		private const TIP_ARRAY:Array = ["是否保存刚才的建筑编辑？","要撤消之前的建造吗？","您确定要拆除该建筑吗？"];
		private const BUTTON_ARRAY:Array = [
			{"CONFIRM":BuildEvent.CONFIRMPANEL_SAVE,"CANCEL":BuildEvent.CONFIRMPANEL_UNDO},
			{"CONFIRM":BuildEvent.CONFIRMPANEL_UNDO,"CANCEL":BuildEvent.CONFIRMPANEL_SAVE},
			{"CONFIRM":BuildEvent.CONFIRMPANEL_REMOVE,"CANCEL":BuildEvent.CONFIRMPANEL_REMAIN}]
		private const CONFIRM:String = "确定";
		private const CANCEL:String = "取消";
	}
}